<?php

class edit_event
{
	var $id;
	var $title;
	var $signupvalue;
	var $champid;
	var $track;
	var $preevent;
	var $postevent;
	var $description;
	var $tracks;
	
	var $add_record;
	var $valid;

	function edit_event($add_record, $champ_id, $tracks, $record='')
	{
		$this->tracks = $tracks;
		
		if($add_record)
		{
			$this->add_record = true;
			$this->id = 0;
			$this->title = '';
			$this->signupvalue = 0;
			$this->track = '';
			$this->description = '';
			$this->preevent = '';
			$this->postevent = '';
			$this->champid = $champ_id;
			$this->valid = true;
		}
		else if(!$add_record)
		{
			$this->add_record = false;
			$this->id = $record['e_id'];
			$this->title = $record['e_title'];
			$this->signupvalue = $record['e_openSignUps'];
			$this->track = $record['e_track'];
			$this->preevent = $record['e_preevent'];
			$this->postevent = $record['e_postevent'];
			$this->description = stripslashes($record['e_description']);
			$this->champid = $champ_id;
			$this->valid = true;
		}
		else
		{
			$this->valid = false;
		}
	}

	function render()
	{		
		global $skin_dir, $lang, $db;
		
		$submitText = $this->add_record ? $lang['AddButton'] : $lang['EditButton'];
		$submitValue = $this->add_record ? 'addRecord' : 'editRecord';
		$remS = $this->add_record ? '<!--' : '';
		$remE = $this->add_record ? '-->' : '';
		
		$title = $this->title;
		$champid = $this->champid;
		$description = $this->description;
		$preevent = $this->preevent;
		$postevent = $this->postevent;
		$id = $this->id;
		
		$activeOptions = '<option value="0" '. ($this->signupvalue==0 ? 'SELECTED' : '').'>No</option>' .
						 '<option value="1" '. ($this->signupvalue==1 ? 'SELECTED' : '').'>Yes</option>';
						 
		$trackOptions = '';
		foreach($this->tracks as $row) 
		{
			$trackOptions = $trackOptions . 
					'<option value="'.$row['t_id'].'" '. ($this->track==$row['t_id'] ? 'SELECTED' : '').'>' . $row['t_fullTitle']. '</option>';
		}
							 
		include $skin_dir . 'admin/editeventtop.tpl';
		$q = 'SELECT d_id, d_title, ed_date, ed_capacity, ed_serverPassword
			  FROM ' . DIVISIONS_TABLE . '
			  LEFT JOIN ' . EVENTDATES_TABLE . ' ON ed_division = d_id AND ed_event = ' . $id . '
			  WHERE d_champid = ' . $champid;
		$result = $db->sql_query($q);
		while($row = $db->sql_fetchrow($result)) 
		{
			$date_name = 'date' . $row['d_id'] . '_' . $id;
			
			$capacity_name = 'capacity' .  $row['d_id'] . '_' . $id;
			$capacity_value = $row['ed_capacity'];
			
			$check_name = 'check' . $row['d_id'] . '_' . $id; 
			$check_value = isset($row['ed_date']) ? 'CHECKED' : '' ;
			
			$password_name = 'password' . $row['d_id'] . '_' . $id; 
			$password_value = $row['ed_serverPassword'];
			
			$date_value = $row['ed_date'];
			$date_title = $row['d_title'];
			include $skin_dir . 'admin/editeventdate.tpl';
		}
		
    	include $skin_dir . 'admin/editeventbottom.tpl';
	}
}
?>